
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient,HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx'
import { environment } from '../../../environments/env.dev';
@Injectable()
export class CommonHttpService {
  private host: string = environment.host;


  constructor(private http: HttpClient) {
  }

  // 使用了拦截器，不再使用options参数
  public options() {
    return {
      headers: new HttpHeaders({
        'Content-Type': 'application/json;charset=UTF-8',
        // 'Content-Type': 'application/x-www-form-urlencoded',
        "Accept": "*/*",
        'Authorization':localStorage.getItem('token')
      })
    };
  }

  get(url: string): Observable<any> {
    return this.http.get(`${this.host}/${url}`);
  }
  get1(url: string): Observable<any> {


    return this.http.get(`${this.host}/${url}`,{headers:{



        'Authorization':localStorage.getItem('token')
        // 'Authorization': "Bear_eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI3IiwiaWF0IjoxNTIwODQ2MTIzLCJzdWIiOiJ7XCJpZFwiOjcsXCJvcGVuaWRcIjpcIm90am5Rd1haMGZWRGdRY0M5VlVUYXFPMGhfTlFcIn0iLCJpc3MiOiJodWl6aGloYW5saW4iLCJleHAiOjE1MjE0NTA5MjN9.UCSEwLUPpBBUmIpOWimn0ol33KIPaO8j5_2hhQlQGY0NM9yQHV"
      }});

  }
  get2(url: string, code: string): Observable<any> {

        return this.http.get(`${this.host}/${url}`,{params:{
            // 'Authorization':localStorage.getItem('token')
            'code': code
          }});
      }

      get3(url: string, code: string): Observable<any> {

            return this.http.get(`${this.host}/${url}`,{headers:{
                // 'Authorization':localStorage.getItem('token')
                'code': code
              }});
            }

  get4(url: string, url1: string): Observable<any> {

    return this.http.get(`${this.host}/${url}`,{params:{
      // 'Authorization':localStorage.getItem('token')
      'url': url1
    }});
  }



  post(url: string, body: any): Observable<any> {
    // return this.http.post(`${this.host}/${url}`,body, this.options());
    return this.http.post(`${this.host}/${url}`,body);
  }

  post1(url: string, body: any): Observable<any> {
    return this.http.post(`${this.host}/${url}`,body,this.options());
    // return this.http.post(`${this.host}/${url}`,body);
  }

  put(url: string, body: any): Observable<any> {
    return this.http.put(`${this.host}/${url}`, body,this.options())
  }

  put1(url: string): Observable<any> {
    return this.http.put(`${this.host}/${url}`,null)
  }

  delete(url: string): Observable<any> {
    return this.http.delete(`${this.host}/${url}`);
  }

   toQueryString(obj) {
    let result = [];
    for (let key in obj) {
      key = encodeURIComponent(key);
      let values = obj[key];
      if (values && values.constructor == Array) {
        let queryValues = [];
        for (let i = 0, len = values.length, value; i < len; i++) {
          value = values[i];
          queryValues.push(this.toQueryPair(key, value));
        }
        result = result.concat(queryValues);
      } else {
        result.push(this.toQueryPair(key, values));
      }
    }
    return result.join('&');
  }
   toQueryPair(key, value) {
    if (typeof value == 'undefined') {
      return key;

    }
    return key + '=' + encodeURIComponent(value === null ? '' : String(value));
  }

//   post2(data) {
//     let headers = new Headers({
//       'Content-Type': 'application/x-www-form-urlencoded'
//     });
//     let options = new RequestOptions({
//       headers: headers
//     });
//     return new Promise((resolve, reject) => {
//       this.http.post(URL, this.toQueryString(data), options )
//         .map(res => res.json())
//         .subscribe(data => resolve(data), err => reject(err))
//     })
//   }
// }
}

